<?php

require_once dirname(__FILE__) . '/../lib/doctor_assistantGeneratorConfiguration.class.php';
require_once dirname(__FILE__) . '/../lib/doctor_assistantGeneratorHelper.class.php';

/**
 * doctor_assistant actions.
 *
 * @package    docplus.co.za
 * @subpackage doctor_assistant
 * @author     Fox Matuku
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class doctor_assistantActions extends autoDoctor_assistantActions
{

    public function preExecute()
    {
        $this->getUser()->setCurrentRoute("doctor_profile");
        if ($this->getUser()->getType() == "Doctor")
        {
            $this->doctorId = $this->getUser()->getDoctor()->getId();
        }
        else
        {
            $this->doctorId = $this->getUser()->getDoctorId();
        }
        parent::preExecute();
    }

    public function executeNew(sfWebRequest $request)
    {
        $this->form = $this->configuration->getForm();
        $this->form->setDefault("doctor_id", $this->doctorId);
        $this->doctor_assistant = $this->form->getObject();
    }

    protected function buildQuery()
    {
        $query = parent::buildQuery();
        $query->addWhere('a.doctor_id = ?', $this->doctorId);
        return $query;
    }

    protected function processForm(sfWebRequest $request, sfForm $form)
    {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid())
        {
            $notice = $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.';

            try
            {
                $doctor_assistant = $form->save();
            }
            catch (Doctrine_Validator_Exception $e)
            {

                $errorStack = $form->getObject()->getErrorStack();

                $message = get_class($form->getObject()) . ' has ' . count($errorStack) . " field" . (count($errorStack) > 1 ? 's' : null) . " with validation errors: ";
                foreach ($errorStack as $field => $errors)
                {
                    $message .= "$field (" . implode(", ", $errors) . "), ";
                }
                $message = trim($message, ', ');

                $this->getUser()->setFlash('error', $message);
                return sfView::SUCCESS;
            }

            $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $doctor_assistant)));

            if ($request->hasParameter('_save_and_add'))
            {
                $this->getUser()->setFlash('notice', $notice . ' You can add another one below.');

                $this->redirect('@doctor_assistant_new');
            }
            else
            {
                $this->getUser()->setFlash('notice', $notice);

                $this->redirect(array('sf_route' => 'doctor_assistant_edit', 'sf_subject' => $doctor_assistant));
            }
        }
        else
        {
            foreach ($this->form as $field)
            {
                echo $field->renderLabel() . " " . $field->renderError() . "<br />";
            }
            die($this->form->renderGlobalErrors());
        }
    }

}
